package domain;

import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;

import dal.ConnectionManager;

public class Report {

	private Project myPro = new Project();
	private Activity myAct = new Activity();
	private Task myTask = new Task();
	private Resource myRes = new Resource();
	private Person myPer = new Person();
	private Material myMat = new Material();
	ConnectionManager mgr = ConnectionManager.getInstance();
	Connection con = mgr.getConnection();
	Statement st;
	private ArrayList<Project> projectList = new ArrayList<Project>();
	private ArrayList<Activity> activityList = new ArrayList<Activity>();
	private ArrayList<Task> taskList = new ArrayList<Task>();
	private ArrayList<Person> personList = new ArrayList<Person>();
	private ArrayList<Material> materialList = new ArrayList<Material>();
	
	
	public Report() {
		super();
	}

	public void setMyPro(Project myPro) {
		this.myPro = myPro;
	}


	public Project getMyPro() {
		return myPro;
	}
	
	public Activity getMyAct() {
		return myAct;
	}



	public void setMyAct(Activity myAct) {
		this.myAct = myAct;
	}



	public Task getMyTask() {
		return myTask;
	}



	public void setMyTask(Task myTask) {
		this.myTask = myTask;
	}



	public Resource getMyRes() {
		return myRes;
	}



	public void setMyRes(Resource myRes) {
		this.myRes = myRes;
	}

	
	public void generateExcelFile()
	{
		projectList = myPro.selectAllProject();
		
		try
		{
		    FileWriter writer = new FileWriter("c:\\report.xls");
	 
		    for(Project pro : projectList)
		    {
			    writer.append("Project Details");
			    writer.append('\n');
			    
			    writer.append("id");
			    writer.append(',');
			    writer.append("name");
			    writer.append(',');
			    writer.append("description");
			    writer.append(',');
			    writer.append("start date");
			    writer.append('\n');
		    
			    writer.append(Integer.toString(pro.getId()));
			    writer.append(',');
			    writer.append(pro.getName());
			    writer.append(',');
			    writer.append(pro.getDesc());
			    writer.append(',');
			    writer.append(pro.getDateToString());
			    writer.append('\n');
			    writer.append('\n');
	 
			    activityList = myAct.selectbyProject(pro);
		    
			    for(Activity act : activityList)
			    {
			    	writer.append("Activity Details");
				    writer.append('\n');
				    
				    writer.append("id");
			    	writer.append(',');
			    	writer.append("name");
			    	writer.append(',');
			    	writer.append("description");
			    	writer.append(',');
			    	writer.append("start date");
			    	writer.append(',');
			    	writer.append("hours");
			    	writer.append(',');
			    	writer.append("deliverable");
			    	writer.append('\n');
			    	
			    	writer.append(Integer.toString(act.getId()));
			    	writer.append(',');
			    	writer.append(act.getName());
			    	writer.append(',');
			    	writer.append(act.getDesc());
			    	writer.append(',');
			    	writer.append(act.getDateToString());
			    	writer.append(',');
			    	writer.append(Integer.toString(act.getHours()));
			    	writer.append(',');
			    	writer.append(act.getDeliverable());
			    	writer.append('\n');
			    	
			    	taskList = myTask.selectbyActivity(act);
			    	
			    	writer.append('\n');
				   
			    	
			    	for(Task task : taskList)
					{
			    		writer.append("Task Details");
						writer.append('\n');
						    
						writer.append("id");
					    writer.append(',');
					    writer.append("name");
					    writer.append(',');
					    writer.append("description");
					    writer.append(',');
					    writer.append("start date");
					    writer.append(',');
					    writer.append("hours");
					    writer.append(',');
					    writer.append("deliverable");
					    writer.append('\n');
			    		
			    		writer.append(Integer.toString(task.getId()));
					    writer.append(',');
					    writer.append(task.getName());
					    writer.append(',');
					    writer.append(task.getDesc());
					    writer.append(',');
					    writer.append(task.getDateToString());
					    writer.append(',');
					    writer.append(Integer.toString(task.getHours()));
					    writer.append(',');
					    writer.append(task.getDeliverable());
					    writer.append('\n');
					    
					    writer.append('\n');

					    myRes = myRes.selectbyTask(task);
				    	
					    if(myRes != null)
					    {
					    
				    	writer.append("Resource Details");
				    	writer.append('\n');
				    	
				    	writer.append("id");
				    	writer.append(',');
				    	writer.append("name");
				    	writer.append(',');
				    	writer.append("start date");
				    	writer.append(',');
				    	writer.append("hours");
				    	writer.append('\n');
				    	
				    	writer.append(Integer.toString(myRes.getResourceId()));
				    	writer.append(',');
				    	writer.append(myRes.getName());
				    	writer.append(',');
				    	writer.append(myRes.getDateToString());
				    	writer.append(',');
				    	writer.append(Integer.toString(myRes.getHours()));
				    	writer.append('\n');
				    	
				    	writer.append('\n');
				    	
				    	personList = myPer.selectbyResource(myRes);
				    	
				    	if(personList != null)
				    	{
				    	
					    	writer.append("Person Details");
				    		writer.append('\n');
				    		
				    		writer.append("id");
				    		writer.append(',');
				    		writer.append("name");
				    		writer.append(',');
				    		writer.append("phone");
				    		writer.append(',');
				    		writer.append("address");
				    		writer.append(',');
				    		writer.append("salary");
				    		writer.append('\n');
				    		
					    	for(Person per : personList)
					    	{
					    		writer.append(Integer.toString(per.getId()));
					    		writer.append(',');
					    		writer.append(per.getName());
					    		writer.append(',');
					    		writer.append(per.getPhone());
					    		writer.append(',');
					    		writer.append(per.getAddress());
					    		writer.append(',');
					    		writer.append(Integer.toString(per.getSalary()));
					    		writer.append('\n');
					    		
					    	}
				    	}
				    	writer.append('\n');
				    	
				    	materialList = myMat.selectbyResource(myRes);
				    	
				    	if(materialList != null)
				    	{
					    	writer.append("Material Details");
					    	writer.append('\n');
					    	
					    	writer.append("id");
					    	writer.append(',');
					    	writer.append("name");
					    	writer.append('\n');
					    	
					    	for(Material mat : materialList)
					    	{
					    		writer.append(Integer.toString(mat.getId()));
						    	writer.append(',');
						    	writer.append(mat.getName());
						    	writer.append('\n');
					    	}
				    	}
				    
					    }
					}
		    	 
		    	 writer.append('\n');
		    }
		}
		    writer.flush();
		    writer.close();
		    }
		catch(IOException e)
		{
		     e.printStackTrace();
		} 
	}   
	


}
